﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DataCentral.Dataset;
using CommonData;

namespace DataCentral.UserControls
{
    public partial class RoleMaintenanceControl : CommonCenterControl
    {
        public RoleMaintenanceControl()
        {
            InitializeComponent();
        }
        public override int SaveDataOnControl()
        {
            int iResult = 0;
            try
            {
                this.Validate();
                this.bsRole.EndEdit();
                iResult = this.tableAdapterManager.UpdateAll(this.dsBET);
                if (iResult < 0)
                {
                    MessageClass.ShowErrorMessage(ConstantClass._Error_SaveDataError);
                }
                else if (this.Parent is GroupControl)
                {
                    string strCaption = ((GroupControl)Parent).Text;
                    ((GroupControl)Parent).Text = strCaption.Substring(0, strCaption.IndexOf(ConstantClass._STR_START));
                    base.IsDataChanged = false;
                    btnSave.Enabled = false;
                }
            }
            catch (Exception exp)
            {
                //CommonFunctions.AddException("Cập nhật quyền không thành công", exp);
            }
            return iResult;
        }


        private void DataHasChanged()
        {
            base.IsDataChanged = true;
            if (this.Parent is GroupControl && !((GroupControl)Parent).Text.EndsWith(ConstantClass._STR_START))
                ((GroupControl)Parent).Text += ConstantClass._STR_START;
            btnSave.Enabled = true;
        }

        private void RoleMaintenanceControl_Load(object sender, EventArgs e)
        {
            daRole.Fill(dsBET.Role);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            SaveDataOnControl();
        }

        private bool IsRoleExisted(DataCentralDataset.RoleRow drRole)
        {
            bool isExist = false;
            foreach (DataCentralDataset.RoleRow drSelect in dsBET.Role)
            {
                if (drSelect.RoleID != drRole.RoleID && drSelect.RoleName.Equals(drRole.RoleName))
                {
                    isExist = true;
                    break;
                }
            }
            return isExist;
        }

        private void viewRole_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
            DataCentralDataset.RoleRow roleRow;

            if (viewRole.GetFocusedDataRow() is DataCentralDataset.RoleRow)
            {
                viewRole.ClearColumnErrors();
                roleRow = (DataCentralDataset.RoleRow)viewRole.GetFocusedDataRow();

                if (roleRow.IsNull("RoleName") || string.IsNullOrEmpty(roleRow.RoleName))
                {
                    viewRole.SetColumnError(colRoleName, ConstantClass._Error_RoleNotBlank);
                    viewRole.FocusedColumn = colRoleName;
                    e.Valid = false;
                }
                else if (!string.IsNullOrEmpty(roleRow.RoleName) && IsRoleExisted(roleRow))
                {
                    viewRole.SetColumnError(colRoleName, ConstantClass._Error_RoleNotExist);
                    viewRole.FocusedColumn = colRoleName;
                    e.Valid = false;
                }
            }
        }

        private void viewRole_RowUpdated(object sender, DevExpress.XtraGrid.Views.Base.RowObjectEventArgs e)
        {
            DataHasChanged();
        }

        private void viewRole_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                btnDelete.PerformClick();
            }
            else if (e.KeyCode == Keys.Insert)
            {
                btnAdd.PerformClick();
            }
            else if (e.Control && e.KeyCode == Keys.S)
            {
                btnSave.PerformClick();
            }
        }

        private void viewRole_InvalidRowException(object sender, DevExpress.XtraGrid.Views.Base.InvalidRowExceptionEventArgs e)
        {
            MessageClass.ShowErrorMessage(ConstantClass._Error_InvalidData);
            e.ExceptionMode = DevExpress.XtraEditors.Controls.ExceptionMode.NoAction;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            DataHasChanged();
        }

        private void viewRole_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            btnDelete.Enabled = (viewRole.GetFocusedDataRow() is DataCentralDataset.RoleRow);
        }
    }
}
